package com.qianniu.napi.admin.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qianniu.napi.admin.entity.PintuanOpen;
import com.qianniu.napi.admin.mapper.PintuanOpenMapper;
import com.qianniu.napi.admin.service.IPintuanOpenService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 开团记录 服务实现类
 * </p>
 *
 * @author chenxuebin
 * @since 2018-07-11
 */
@Service
public class PintuanOpenServiceImpl extends ServiceImpl<PintuanOpenMapper, PintuanOpen> implements IPintuanOpenService {

    private static final Logger logger = LoggerFactory.getLogger(PintuanOpenServiceImpl.class);

    @Autowired
    private PintuanOpenMapper pintuanOpenMapper;

    public List<PintuanOpen> getPTOpenPage(Long mid, int pageSize, int pageIndex, int status)
    {

        Page page = new Page(pageIndex, pageSize);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("mid",mid);
        map.put("status",status);

        int total =  pintuanOpenMapper.selectCount(new EntityWrapper<PintuanOpen>().eq("mid",mid).eq("status",status));

        page.setTotal(total);

        logger.info("page.getOffset()="+page.getOffset());
        logger.info("page.getLimit()="+page.getLimit());

        map.put("size",pageSize);
        map.put("skip",page.getOffset());

        List<PintuanOpen> list = pintuanOpenMapper.selectPTOpenPage(map);
        Date now = new Date();
        for(PintuanOpen item: list){
            if(status==0){
                item.setShowCountdownMilliseconds(item.getStarttime().getTime()-now.getTime());
            }else if(status==1){
                item.setShowCountdownMilliseconds(item.getEndtime().getTime()-now.getTime());
            }
        }

        return list;
    }
}
